package dao.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.LeaveDTO;
import model.PositionDTO;
import model.UserDTO;
import exception.DAOSystemException;
import dao.LeaveDAO1;

public class LeaveDAOImpl1 implements LeaveDAO1 {
	MySqlImpl mysqlImpl = new MySqlImpl();
	@Override
	
	public void updateForApprove(String userid,Double consume,String type) throws DAOSystemException
	{
		// TODO Auto-generated method stub
		String updateUser=null;
		
		if((type.trim()).equalsIgnoreCase("Annual Leave"))
		{
		  updateUser="UPDATE user set consumeAnnualAllowance = "+consume+
		  		      " Where id='"+userid+"';";
		}
		else if((type.trim()).equalsIgnoreCase("Medical Leave"))
		{
		  updateUser="UPDATE user set consumeMedicalAllowance = "+consume+
		  		      " Where id='"+userid+"';";
		}                

try {

	Logger.getLogger(getClass().getName()).log(Level.INFO,
			"Executing update: " + updateUser);	
    int res1 = mysqlImpl.execute (updateUser);
   } catch (Exception e) {
	String error = "Error selecting leave. Nested Exception is: "
			+ e;
	Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
	throw new DAOSystemException(error);
    } finally {
	try {
		mysqlImpl.close();
	} catch (Exception e) {
	}
    }
	 updateLeaveForApprove(userid);	
	}
	
	public void updateLeaveForApprove(String id) throws DAOSystemException
	{
		
		String updateLeave ="UPDATE `leaveapplicationdb`.`leave` SET status='Approve'"+		
		                   " WHERE userId='"+id+"';";
		
		try {
		
			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing update: " + updateLeave);
			int res = mysqlImpl.execute(updateLeave);
		    
		   } catch (Exception e) {
				String error = "Error selecting leave. Nested Exception is: "
					+ e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		    } finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		    }
	}
	public void updateLeaveForReject(String id) throws DAOSystemException
	{
		
		String updateLeave ="UPDATE `leaveapplicationdb`.`leave` SET status='Reject'"+		
		                   " WHERE userId='"+id+"';";
		
		try {
		
			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing update: " + updateLeave);
			int res = mysqlImpl.execute(updateLeave);
		    
		   } catch (Exception e) {
				String error = "Error selecting leave. Nested Exception is: "
					+ e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		    } finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		    }
	}
	
	}


